﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Timers;
using System.Xml;
using System.IO;
using System.Text;


namespace FUSE
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class TitleScreen : GameScene/*Microsoft.Xna.Framework.DrawableGameComponent*/
    {
        protected TextMenuComponent titleMenu;
        protected TextMenuComponent playMenu;
        protected TextMenuComponent optionsMenu;
        protected TextMenuComponent highscoreMenu;
        protected TextMenuComponent difficultyMenu;

        private Boolean hasSelectedDifficulty = false;
        private MapGenerator.Difficulty selectedDifficulty = MapGenerator.Difficulty.Tutorial;

        KeyboardState oldKeyboardState;
        Boolean hasSelected = true;
        int currentScreenSelection;
        public int ScreenSelection;

        private bool doneOnce;

        private Vector2 currentPlayPosition;
        private Vector2 currentBuildPosition;
        private Vector2 currentHighScorePosition;
        private Vector2 currentExitPosition;
        private Vector2 currentPPEPosition;

        public const String BUTTON_FONT = "scoreFont";
        private SpriteFont scoreFont;
        Boolean menuStart = false;
        Boolean flashText = true;
        public Timer timer;
        public DateTime startTime;
        public TimeSpan elapsedTime;
        float timeCheck = 0;

        Sprite background;
        Sprite startButton;
        Sprite highScoreButton;
        Sprite exitButton;
        Sprite optionsButton;
        Sprite menuBackground;

        //button textures
        Texture2D startTex;
        Texture2D highscoreTex;
        Texture2D exitTex;
        Texture2D optionsTex;
        Texture2D startHTex;
        Texture2D highscoreHTex;
        Texture2D exitHTex;
        Texture2D optionsHTex;


        //Game game;
        SpriteBatch sb;
        GraphicsDeviceManager graphics;

        private enum TitleMenuSelection
        {
            Play = 0,
            Options,
            //HighScore,
            Exit
        }

        private enum PlaySelection
        {
            Tutorial = 0,
            Survival,
            Back
        }

        private enum OptionsSelection
        {
            Difficulty = 0,
            ResetDifficulty,
            Back
        }



        public TitleScreen(Game game) : base(game)
        {
            graphics = this.Game.Services.GetService(typeof(GraphicsDeviceManager)) as GraphicsDeviceManager;
            sb = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            
            ContentManager content = Game.Content;
            // Load game font
            scoreFont = content.Load<SpriteFont>(BUTTON_FONT);

            SpriteFont smallFont = content.Load<SpriteFont>("smallMenuFont");
            SpriteFont largeFont = content.Load<SpriteFont>("largeMenuFont");

            string[] titleItems = { "Play", "Options", /*"Highscore",*/ "Exit" };
            titleMenu = new TextMenuComponent(game, smallFont, largeFont);
            titleMenu.SetMenuItems(titleItems);
            Components.Add(titleMenu);

            string[] playItems = { "Tutorial", "Survival", "Back" };
            playMenu = new TextMenuComponent(game, smallFont, largeFont);
            playMenu.SetMenuItems(playItems);
            Components.Add(playMenu);

            string[] optionItems = { "Choose Difficulty", "Reset Difficulty", "Back" };
            optionsMenu = new TextMenuComponent(game, smallFont, largeFont);
            optionsMenu.SetMenuItems(optionItems);
            Components.Add(optionsMenu);

            string[] highscoreItems = { "Back" };
            highscoreMenu = new TextMenuComponent(game, smallFont, largeFont);
            highscoreMenu.SetMenuItems(highscoreItems);
            Components.Add(highscoreMenu);

            string[] difficultyItems = { "Noob", "Easy", "Medium", "Hard", "Insane", "AreYouSerious" };
            difficultyMenu = new TextMenuComponent(game, smallFont, largeFont);
            difficultyMenu.SetMenuItems(difficultyItems);
            Components.Add(difficultyMenu);

            int properY = 250;
           
            
            //Buttons in the middle
            currentPlayPosition = new Vector2((300) - (((int)scoreFont.MeasureString("Play").X / 2)), (properY + ((int)scoreFont.MeasureString("Play").Y - 60)));
            currentBuildPosition = new Vector2((300) - (((int)scoreFont.MeasureString("Build").X / 2)), (properY + ((int)scoreFont.MeasureString("Options").Y  - 30)));
            currentHighScorePosition = new Vector2((300) - (((int)scoreFont.MeasureString("High Score").X / 2)), (properY + ((int)scoreFont.MeasureString("High Score").Y)));
            //currentOptionsPosition = new Vector2((300) - (((int)scoreFont.MeasureString("Options").X / 2)), (properY + ((int)scoreFont.MeasureString("Options").Y + 30)));
            currentExitPosition = new Vector2((300) - (((int)scoreFont.MeasureString("Exit").X / 2)), (properY + ((int)scoreFont.MeasureString("Exit").Y + 60)));
            currentPPEPosition = new Vector2(((graphics.PreferredBackBufferWidth/2)-145), (properY+120));
            
            this.Visible = false;

            this.Enabled = false;
        }

        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            background.LoadContent(Game.Content, "DisplayMaybeNewMech");
            menuBackground.LoadContent(Game.Content, "DisplayMaybeNewMechOverlay");
            
            //ScaleBackground(background);
            //ScaleBackground(menuBackground);

            

            //load sprites
            startButton.LoadContent(Game.Content, "startButtonTex");
            optionsButton.LoadContent(Game.Content, "optionsButtonTex");
            highScoreButton.LoadContent(Game.Content, "highscoreButtonTex");
            exitButton.LoadContent(Game.Content, "exitButtonTex");

            //load textures
            startTex = Game.Content.Load<Texture2D>("playUnhigh");
            highscoreTex = Game.Content.Load<Texture2D>("hscoreUnhigh");
            exitTex = Game.Content.Load<Texture2D>("exitHigh");
            optionsTex = Game.Content.Load<Texture2D>("optionsUnhigh");
            startHTex = Game.Content.Load<Texture2D>("playHigh");
            highscoreHTex = Game.Content.Load<Texture2D>("hscoreHigh");
            exitHTex = Game.Content.Load<Texture2D>("exitUnhigh");
            optionsHTex = Game.Content.Load<Texture2D>("optionsButtonHighlightedTex");

            

            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            doneOnce = true;
            
            
            // TODO: Add your initialization code here
            //sb = newSB;
            //sb = new SpriteBatch(graphics.GraphicsDevice);
            //playerInput = new KeyboardState();
            //hasSelected = false;         
            ScreenSelection = MyConstants.ITitleScreen; //which menu item is selected default is 0
            currentScreenSelection = 0;

            background = new Sprite(graphics);
            menuBackground = new Sprite(graphics);
            startButton = new Sprite(graphics);
            highScoreButton = new Sprite(graphics);
            exitButton = new Sprite(graphics);
            optionsButton = new Sprite(graphics);



            //set background position
            background.SetX(0);
            background.SetY(0);
            //ScaleBackground(background);

                       
           
            base.Initialize();

        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here
            if ((Game.Services.GetService(typeof(MapGenerator)) as MapGenerator).mapList.Count > 0)
            {

                //Checks if user changes menu selection
                UpdateMenuSelect();

                flashText = false;
                //Draw();
                timeCheck += (float)gameTime.ElapsedGameTime.TotalSeconds;
                if (timeCheck > 0.5)
                {
                    flashText = true;
                }
                if (timeCheck > 1)
                {
                    timeCheck -= 1;
                }

                //get old keyboardstate
                oldKeyboardState = Keyboard.GetState();
            }
                
            base.Update(gameTime);
            
        }

        public override void Draw(GameTime gameTime)
        {
            if ((Game.Services.GetService(typeof(MapGenerator)) as MapGenerator).mapList.Count >=
                (Game.Services.GetService(typeof(MapGenerator)) as MapGenerator).numberOfMaps)
            {
                if (doneOnce)
                {
                    ScaleBackground(background);
                    ScaleBackground(menuBackground);
                    doneOnce = false;
                }

                //sb.Begin();

                //draw background
                //
                //graphics.PreferredBackBufferWidth = background.Size.Width;
                //graphics.PreferredBackBufferHeight = background.Size.Height;
                //graphics.GraphicsDevice.PresentationParameters.BackBufferHeight = background.Size.Height;
                //graphics.GraphicsDevice.PresentationParameters.BackBufferWidth = background.Size.Width;
                background.Draw(sb);

                if (menuStart == true)
                {
                    //DrawMenuButtons(sb);
                    menuBackground.Draw(sb);
                    base.Draw(gameTime);
                    //this.titleMenu.Visible = true;
                    //this.titleMenu.Enabled = true;
                }
                else
                {
                    if (flashText)
                    {
                        sb.Begin();
                        sb.DrawString(scoreFont, "Please Press Enter...", currentPPEPosition, Color.White);
                        sb.End();
                    }
                    else
                    {
                        sb.Begin();
                        sb.DrawString(scoreFont, "Please Press Enter...", currentPPEPosition, Color.LimeGreen);
                        sb.End();
                    }

                }

                //background.Draw(sb);

                //SetButtonLocation();

                //updates the selection effects
                //DrawMenuButtons(sb);

                //sb.End();
            }
            else
            {
                SpriteBatch spriteBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));

                spriteBatch.Begin();

                String BUTTON_FONT = "scoreFont";
                ContentManager cm = Game.Content;
                SpriteFont sf = cm.Load<SpriteFont>(BUTTON_FONT);

                timeCheck += (float)gameTime.ElapsedGameTime.TotalSeconds;

                if ((timeCheck > 0.5 && timeCheck < 1))
                {
                    spriteBatch.DrawString(sf, "Loading", new Vector2(200, 200), Color.Azure);
                }
                if (timeCheck >= 1 && timeCheck < 1.5)
                {
                    spriteBatch.DrawString(sf, "Loading.", new Vector2(200, 200), Color.Azure);
                }
                if (timeCheck >= 1.5 && timeCheck < 2)
                {
                    spriteBatch.DrawString(sf, "Loading..", new Vector2(200, 200), Color.Azure);
                }
                if (timeCheck >= 2 && timeCheck < 2.5)
                {
                    spriteBatch.DrawString(sf, "Loading...", new Vector2(200, 200), Color.Azure);
                    timeCheck = 0;
                }
                spriteBatch.End();
            }
        }
        
        //scale the background image to the window size
        private void ScaleBackground(Sprite backgroundSprite)
        {
            Vector2 tempScale;
            //float heightRatio;
            //float widthRatio; 
            //tempScale.Y = ((float)this.graphics.PreferredBackBufferHeight / (float)backgroundSprite.Size.Height);
            //tempScale.X = ((float)this.graphics.PreferredBackBufferWidth / (float)backgroundSprite.Size.Width);
            tempScale.Y = ((float)backgroundSprite.Size.Height / (float)this.graphics.PreferredBackBufferHeight);
            tempScale.X = ((float)backgroundSprite.Size.Width / (float)this.graphics.PreferredBackBufferWidth);
            //float widthRatio = (float)(800f / 600f);
            graphics.PreferredBackBufferHeight = (int)((float)graphics.PreferredBackBufferHeight * tempScale.Y);
            graphics.PreferredBackBufferWidth =  (int)((float)graphics.PreferredBackBufferWidth * tempScale.X);
            graphics.ApplyChanges();
            //backgroundSprite.SetScale(tempScale);
            //backgroundSprite.SetScale((heightRatio + widthRatio) / 2.0f);
        }

        //updates the graphic effects associated with the menu items
        private void DrawMenuButtons(SpriteBatch sb)
        {
            menuBackground.Draw(sb);

            sb.Begin();
            /**
            sb.DrawString(scoreFont, "Play", currentPlayPosition, Color.LimeGreen);
            sb.DrawString(scoreFont, "Options", currentBuildPosition, Color.LimeGreen);
            sb.DrawString(scoreFont, "High Score", currentHighScorePosition, Color.LimeGreen);
            sb.DrawString(scoreFont, "Exit", currentExitPosition, Color.LimeGreen);
            sb.End();
            **/

            
            //draw newgame button
            if (currentScreenSelection == 0)
            {
                if (flashText)
                {
                    sb.DrawString(scoreFont, "Play", currentPlayPosition, Color.LimeGreen);
                }
                else
                {
                    sb.DrawString(scoreFont, "Play", currentPlayPosition, Color.White);
                }
            }
            else
            {
                sb.DrawString(scoreFont, "Play", currentPlayPosition, Color.LimeGreen);
            }

            //draw options button
            if (currentScreenSelection == 1)
            {
                if (flashText)
                {
                    sb.DrawString(scoreFont, "Build", currentBuildPosition, Color.LimeGreen);
                }
                else
                {
                    sb.DrawString(scoreFont, "Build", currentBuildPosition, Color.White);
                }
            }
            else
            {
                sb.DrawString(scoreFont, "Build", currentBuildPosition, Color.LimeGreen);
            }

            //draw tutorial button
            if (currentScreenSelection == 2)
            {
                if (flashText)
                {
                    sb.DrawString(scoreFont, "High Score", currentHighScorePosition, Color.LimeGreen);
                }
                else
                {
                    sb.DrawString(scoreFont, "High Score", currentHighScorePosition, Color.White);
                };
            }
            else
            {
                sb.DrawString(scoreFont, "High Score", currentHighScorePosition, Color.LimeGreen);
            }

            //draw load button
            if (currentScreenSelection == 3)
            {
                if (flashText)
                {
                    sb.DrawString(scoreFont, "Exit", currentExitPosition, Color.LimeGreen);
                }
                else
                {
                    sb.DrawString(scoreFont, "Exit", currentExitPosition, Color.White);
                }
            }
            else
            {
                sb.DrawString(scoreFont, "Exit", currentExitPosition, Color.LimeGreen);
            }

            sb.End();
        }
        
        //increments or decrements the menu selection based on key press
        private void UpdateMenuSelect()
        {
            if (menuStart == true)
            {

                /*if (hasSelected == true)
                {
                    
                    if ((Keyboard.GetState().IsKeyDown(Keys.Up) && oldKeyboardState.IsKeyUp(Keys.Up)) ||
                        (Keyboard.GetState().IsKeyDown(Keys.Right) && oldKeyboardState.IsKeyUp(Keys.Right)))
                    {
                        //decrements player selection
                        if (currentScreenSelection > 0)
                        {
                            currentScreenSelection--;
                        }
                    }
                    else if ((Keyboard.GetState().IsKeyDown(Keys.Down) && oldKeyboardState.IsKeyUp(Keys.Down)) ||
                        (Keyboard.GetState().IsKeyDown(Keys.Left) && oldKeyboardState.IsKeyUp(Keys.Left)))
                    {
                        // 3 is the last item
                        if (currentScreenSelection < 3)
                        {
                            currentScreenSelection++;
                        }
                    }
                    else if ((Keyboard.GetState().IsKeyUp(Keys.Enter) && oldKeyboardState.IsKeyDown(Keys.Enter)) ||
                        (Keyboard.GetState().IsKeyUp(Keys.Space) && oldKeyboardState.IsKeyDown(Keys.Space)))
                    {
                        //oldKeyboardState = new KeyboardState();
                        
                        ScreenSelection = currentScreenSelection;
                        hasSelected = true;
                        doneOnce = true; 
                        this.Enabled = false;
                        this.Visible = false;
                        currentScreenSelection = MyConstants.ITitleScreen;
                            
                    }
                }
               /**
                else
                {
                    // note: fix it better
                    hasSelected = false;
                    //screen selected update global
                    ScreenSelection = currentScreenSelection;

                }
                * */
                if (titleMenu.Enabled && titleMenu.Visible)
                {
                    if ((Keyboard.GetState().IsKeyUp(Keys.Space) && oldKeyboardState.IsKeyDown(Keys.Space)) ||
                        (Keyboard.GetState().IsKeyUp(Keys.Enter) && oldKeyboardState.IsKeyDown(Keys.Enter)))
                    {
                        if (titleMenu.SelectedIndex == (int)TitleMenuSelection.Play)
                        {
                            titleMenu.Hide();
                            playMenu.Show();
                        }
                        else if (titleMenu.SelectedIndex == (int)TitleMenuSelection.Options)
                        {
                            titleMenu.Hide();
                            optionsMenu.Show();
                        }
                        /*else if (titleMenu.SelectedIndex == (int)TitleMenuSelection.HighScore)
                        {
                            titleMenu.Hide();
                            //also display highscore from services
                            highscoreMenu.Show();
                        }*/
                        else if (titleMenu.SelectedIndex == (int)TitleMenuSelection.Exit)
                        {
                            Game.Exit();
                        }
                    }
                }
                else if (playMenu.Enabled && playMenu.Visible)
                {
                    if ((Keyboard.GetState().IsKeyUp(Keys.Space) && oldKeyboardState.IsKeyDown(Keys.Space)) ||
                        (Keyboard.GetState().IsKeyUp(Keys.Enter) && oldKeyboardState.IsKeyDown(Keys.Enter)))
                    {
                        MapGenerator mapGenerator = ((MapGenerator)this.Game.Services.GetService(typeof(MapGenerator)));
                        if (playMenu.SelectedIndex == (int)PlaySelection.Survival)
                        {
                            //go to survival mode
                            if (hasSelectedDifficulty)
                            {
                                mapGenerator.RestartSurvival(this.selectedDifficulty);
                            }
                            else
                            {
                                mapGenerator.RestartSurvival(MapGenerator.Difficulty.Noob);
                            }

                            (this.Game.Services.GetService(typeof(Player)) as Player).reInitialize(0);
                             
                            foreach (Galaxy item in Game.Components.OfType<Galaxy>())
                            {
                                item.ResetLoadingFlags();
                                item.tInitialize();
                            }

                            this.Hide();
                            foreach (CreationScreen item in Game.Components.OfType<CreationScreen>())
                            {
                                item.Show();
                            }
                        }
                        else if (playMenu.SelectedIndex == (int)PlaySelection.Tutorial)
                        {
                            (this.Game.Services.GetService(typeof(Player)) as Player).reInitialize(0);

                            //go to tutorial mode
                            mapGenerator.RestartTutorial();

                            foreach (Galaxy item in Game.Components.OfType<Galaxy>())
                            {
                                item.ResetLoadingFlags();
                                item.tInitialize();
                            }

                            this.Hide();
                            foreach (CreationScreen item in Game.Components.OfType<CreationScreen>())
                            {
                                item.Show();
                            }
                        }
                        else if (playMenu.SelectedIndex == (int)PlaySelection.Back)
                        {
                            playMenu.Hide();
                            titleMenu.Show();
                        }
                    }
                    else if (Keyboard.GetState().IsKeyUp(Keys.Back) && oldKeyboardState.IsKeyDown(Keys.Back))
                    {
                        playMenu.Hide();
                        titleMenu.Show();
                    }
                }
                else if (optionsMenu.Enabled && optionsMenu.Visible)
                {
                    if ((Keyboard.GetState().IsKeyUp(Keys.Space) && oldKeyboardState.IsKeyDown(Keys.Space)) ||
                        (Keyboard.GetState().IsKeyUp(Keys.Enter) && oldKeyboardState.IsKeyDown(Keys.Enter)))
                    {
                        if (optionsMenu.SelectedIndex == (int)OptionsSelection.Difficulty)
                        {
                            optionsMenu.Hide();
                            difficultyMenu.Show();
                        }
                        else if (optionsMenu.SelectedIndex == (int)OptionsSelection.ResetDifficulty)
                        {
                            this.hasSelectedDifficulty = false;
                            this.selectedDifficulty = MapGenerator.Difficulty.Noob;
                        }
                        /*else if (optionsMenu.SelectedIndex == (int)OptionsSelection.Sound)
                        {
                            //go to sound menu
                        }*/
                        else if (optionsMenu.SelectedIndex == (int)OptionsSelection.Back)
                        {
                            optionsMenu.Hide();
                            titleMenu.Show();
                        }

                    }
                    else if (Keyboard.GetState().IsKeyUp(Keys.Back) && oldKeyboardState.IsKeyDown(Keys.Back))
                    {
                        optionsMenu.Hide();
                        titleMenu.Show();
                    }
                }
                /*else if (highscoreMenu.Enabled && highscoreMenu.Enabled)
                {
                    if ((Keyboard.GetState().IsKeyUp(Keys.Space) && oldKeyboardState.IsKeyDown(Keys.Space)) ||
                        (Keyboard.GetState().IsKeyUp(Keys.Enter) && oldKeyboardState.IsKeyDown(Keys.Enter)))
                    {
                        highscoreMenu.Hide();
                        titleMenu.Show();
                    }
                    else if (Keyboard.GetState().IsKeyUp(Keys.Back) && oldKeyboardState.IsKeyDown(Keys.Back))
                    {
                        highscoreMenu.Hide();
                        titleMenu.Show();
                    }
                }*/
                else if (difficultyMenu.Enabled && difficultyMenu.Visible)
                {
                    if ((Keyboard.GetState().IsKeyUp(Keys.Space) && oldKeyboardState.IsKeyDown(Keys.Space)) ||
                        (Keyboard.GetState().IsKeyUp(Keys.Enter) && oldKeyboardState.IsKeyDown(Keys.Enter)))
                    {
                        MapGenerator mapGenerator = ((MapGenerator)this.Game.Services.GetService(typeof(MapGenerator)));

                        if (difficultyMenu.SelectedIndex == ((int)MapGenerator.Difficulty.Noob - 1))
                        {
                            //mapGenerator.setDifficulty(MapGenerator.Difficulty.Noob);
                            this.selectedDifficulty = MapGenerator.Difficulty.Noob;
                        }
                        else if (difficultyMenu.SelectedIndex == ((int)MapGenerator.Difficulty.Easy - 1))
                        {
                            //mapGenerator.setDifficulty(MapGenerator.Difficulty.Easy);
                            this.selectedDifficulty = MapGenerator.Difficulty.Easy;
                        }
                        else if (difficultyMenu.SelectedIndex == ((int)MapGenerator.Difficulty.Medium - 1))
                        {
                            //mapGenerator.setDifficulty(MapGenerator.Difficulty.Medium);
                            this.selectedDifficulty = MapGenerator.Difficulty.Medium;
                        }
                        else if (difficultyMenu.SelectedIndex == ((int)MapGenerator.Difficulty.Hard - 1))
                        {
                            //mapGenerator.setDifficulty(MapGenerator.Difficulty.Hard);
                            this.selectedDifficulty = MapGenerator.Difficulty.Hard;
                        }
                        else if (difficultyMenu.SelectedIndex == ((int)MapGenerator.Difficulty.Insane - 1))
                        {
                            //mapGenerator.setDifficulty(MapGenerator.Difficulty.Insane);
                            this.selectedDifficulty = MapGenerator.Difficulty.Insane;
                        }
                        else if (difficultyMenu.SelectedIndex == ((int)MapGenerator.Difficulty.AreYouSerious - 1))
                        {
                            //mapGenerator.setDifficulty(MapGenerator.Difficulty.AreYouSerious);
                            this.selectedDifficulty = MapGenerator.Difficulty.AreYouSerious;
                        }

                        (this.Game.Services.GetService(typeof(Player)) as Player).SelectedDifficulty = this.selectedDifficulty;

                        this.hasSelectedDifficulty = true;
                        //this.selectedDifficulty = mapGenerator.currDifficulty;
                        //mapGenerator.Reset();

                        /*
                        foreach (Galaxy item in Game.Components.OfType<Galaxy>())
                        {
                            item.ResetLoadingFlags();
                            item.tInitialize();
                        }*/

                        difficultyMenu.Hide();
                        optionsMenu.Show();
                    }
                    else if (Keyboard.GetState().IsKeyUp(Keys.Back) && oldKeyboardState.IsKeyDown(Keys.Back))
                    {
                        difficultyMenu.Hide();
                        optionsMenu.Show();
                    }
                }
            }
            else
            {
                if((Keyboard.GetState().IsKeyUp(Keys.Enter) && oldKeyboardState.IsKeyDown(Keys.Enter)))
                {
                    menuStart = true;

                    this.titleMenu.Position = new Vector2((Game.Window.ClientBounds.Width - titleMenu.Width) / 2,
                        (Game.Window.ClientBounds.Height - titleMenu.Height) / 2);

                    this.optionsMenu.Position = new Vector2((Game.Window.ClientBounds.Width - optionsMenu.Width) / 2,
                        (Game.Window.ClientBounds.Height - optionsMenu.Height) / 2);

                    this.highscoreMenu.Position = new Vector2((Game.Window.ClientBounds.Width - highscoreMenu.Width) / 2,
                        (Game.Window.ClientBounds.Height - highscoreMenu.Height) / 2);

                    this.playMenu.Position = new Vector2((Game.Window.ClientBounds.Width - playMenu.Width) / 2,
                        (Game.Window.ClientBounds.Height - playMenu.Height) / 2);

                    this.difficultyMenu.Position = new Vector2((Game.Window.ClientBounds.Width - difficultyMenu.Width) / 2,
                        (Game.Window.ClientBounds.Height - difficultyMenu.Height) / 2);

                    titleMenu.Show();
                }
            }
        }

        public void SetButtonLocation()
        {
            

            //TODO calcuate button offset for each button position
            int buttonXOffset = (this.graphics.PreferredBackBufferWidth * 1 / 6);
            int buttonYOffset = (this.graphics.PreferredBackBufferHeight * 2/3);

            //set new game button location
            startButton.SetX((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Width / 2) - (startButton.Size.Width / 2)), 2)) + (buttonXOffset * 1)));
            startButton.SetY((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Height / 2) - (startButton.Size.Height / 2)), 2)) + (buttonYOffset * 1.275)));

            //set option button location
            optionsButton.SetX((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Width / 2) - (optionsButton.Size.Width / 2)), 2)) + (buttonXOffset * 2)));
            optionsButton.SetY((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Height / 2) - (optionsButton.Size.Height / 2)), 2)) + (buttonYOffset*1.275)));

            //set tutorial button location
            highScoreButton.SetX((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Width / 2) - (highScoreButton.Size.Width / 2)), 2)) + (buttonXOffset * 3)));
            highScoreButton.SetY((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Height / 2) - (highScoreButton.Size.Height / 2)), 2)) + (buttonYOffset*1.265)));

            //set load button location
            exitButton.SetX((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Width / 2) - (exitButton.Size.Width / 2)), 2)) + (buttonXOffset * 4)));
            exitButton.SetY((int)(Math.Sqrt(
                Math.Pow(((startButton.Size.Height / 2) - (exitButton.Size.Height / 2)), 2)) + (buttonYOffset*1.275)));

        }

        public string getTime()
        {
            string TimeInString = "";
            elapsedTime = DateTime.Now.Subtract(startTime);
            if (elapsedTime.ToString().Length > 11)
            {
                TimeInString = elapsedTime.ToString().Substring(0, 11);
            }
            else
            {
                TimeInString = elapsedTime.ToString();
            }

            return TimeInString;
        }


        public int getPlayerSelection()
        {
            return ScreenSelection;
        }

        public void returnToPlayMenu()
        {
            doneOnce = true;
            menuStart = true;

            this.Hide();
            this.Enabled = true;
            this.Visible = true;

            this.playMenu.Show();
        }

        public override void Show()
        {
            doneOnce = true;
            menuStart = false;

            this.titleMenu.Hide();
            this.titleMenu.Position = new Vector2((Game.Window.ClientBounds.Width - titleMenu.Width) / 2,
                (Game.Window.ClientBounds.Height - titleMenu.Height) / 2);

            this.optionsMenu.Hide();
            this.optionsMenu.Position = new Vector2((Game.Window.ClientBounds.Width - optionsMenu.Width) / 2,
                (Game.Window.ClientBounds.Height - optionsMenu.Height) / 2);

            this.highscoreMenu.Hide();
            this.highscoreMenu.Position = new Vector2((Game.Window.ClientBounds.Width - highscoreMenu.Width) / 2,
                (Game.Window.ClientBounds.Height - highscoreMenu.Height) / 2);

            this.playMenu.Hide();
            this.playMenu.Position = new Vector2((Game.Window.ClientBounds.Width - playMenu.Width) / 2,
                (Game.Window.ClientBounds.Height - playMenu.Height) / 2);

            this.difficultyMenu.Hide();
            this.difficultyMenu.Position = new Vector2((Game.Window.ClientBounds.Width - difficultyMenu.Width) / 2,
                (Game.Window.ClientBounds.Height - difficultyMenu.Height) / 2);

            this.Enabled = true;
            this.Visible = true;

            base.Show();
        }

        public override void Hide()
        {
            this.titleMenu.Hide();

            this.optionsMenu.Hide();

            this.highscoreMenu.Hide();

            this.playMenu.Hide();

            this.difficultyMenu.Hide();

            this.Enabled = false;
            this.Visible = false;

            base.Hide();
        }

        public String getHighscores()
        {
            /*XmlTextReader reader = new XmlTextReader("../../../HighScores.xml");
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        break;
                    case XmlNodeType.Text:
                        break;
                    case XmlNodeType.EndElement:
                        break;
                }
            }*/
            return "";
        }

        public void writeHighscore(String username, double highscore)
        {
            XmlDocument highscoresXml = new XmlDocument();
            //highscoresXml.Load("../../../HighScores.xml");
            XmlNodeList entries = highscoresXml.GetElementsByTagName("");


            //XmlTextReader reader = new XmlTextReader("../../../HighScores.xml");
            /*while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        break;
                    case XmlNodeType.Text:
                        break;
                    case XmlNodeType.EndElement:
                        break;
                }
            }*/

            //XmlTextWriter writer = new XmlTextWriter(new StreamWriter("../../../HighScores.xml"));
            //writer.

        }
            
    }
}
